package com.didibigdata.Repositories;

import com.didibigdata.entities.OrderODGPS;
import com.didibigdata.utils.GPSUtil;
import com.didibigdata.utils.JDBCTools;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.util.ArrayList;
import java.util.List;

/**
 * Created by lkabb on 2020/6/7
 */
public class HeatMapRepository {

    public List<OrderODGPS> getOrderODGPS(String selectTable) {
        //日志
        Logger logger = LoggerFactory.getLogger(HeatMapRepository.class);
        logger.info("add HeatMapRepository logger successful!");

        ResultSet resultSetStartmysql = null;
        Connection connectionmysql = null;
        PreparedStatement statementmysql = null;
        List<OrderODGPS> orderStartList = new ArrayList<OrderODGPS>();

        try {
            connectionmysql = JDBCTools.getConnection();
            String sqltime = "select * from " + selectTable;
            statementmysql = connectionmysql.prepareStatement(sqltime);
            logger.info("connect to JDBC and executeQuery [{}]", sqltime);
            resultSetStartmysql = statementmysql.executeQuery();
            while (resultSetStartmysql.next()) {
                OrderODGPS orderStart = new OrderODGPS();
                GPSUtil gps = new GPSUtil();
                double[] GtoB = new double[2];
                GtoB = gps.gcj02_To_Bd09(Double.valueOf(resultSetStartmysql.getString(3)),Double.valueOf(resultSetStartmysql.getString(4)));
                orderStart.setLongitude(GtoB[0]);
                orderStart.setLatitude(GtoB[1]);
                orderStart.setWeights(100);
                orderStartList.add(orderStart);
            }

        } catch (Exception e) {
            logger.error("HeatMapRepository SQLException: [{}]", e);
            e.printStackTrace();
        } finally {
            JDBCTools.releaseConnection(connectionmysql, statementmysql, resultSetStartmysql);
        }

        return orderStartList;
    }
}
